package com.espier.t9keyboard;

import java.io.Serializable;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract;

public class ContactPerson implements Serializable {

	/**
     * 
     */
	private static final long serialVersionUID = 1L;
	private String name;
	private String sort_key;
	private String photo_id;
	private List<ContactPhone> phone;
	private Context context;
	private int simType = 0;
	private String contactId;
	private String starred;
	private String lookup;
	private String isPrimary;


    public String getIsPrimary() {
        return isPrimary;
    }

    public void setIsPrimary(String isPrimary) {
        this.isPrimary = isPrimary;
    }

    public String getLookup() {
		return lookup;
	}

	public void setLookup(String lookup) {
		this.lookup = lookup;
	}

	public String getStarred() {
		return starred;
	}

	public void setStarred(String starred) {
		this.starred = starred;
	}

	public String getContactId() {
		return contactId;
	}

	public void setContactId(String contactId) {
		this.contactId = contactId;
	}

	public int getSimType() {
		return simType;
	}

	public void setSimType(int simType) {
		this.simType = simType;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSortKey() {
		return this.sort_key;
	}

	public void setSortKey(String sort_key) {
		this.sort_key = sort_key;
	}

	public String getPhotoId() {
		return photo_id;
	}

	public void setPhotoId(String photo_id) {
		this.photo_id = photo_id;
	}

	public List<ContactPhone> getPhone() {
		return phone;
	}

	public void setPhone(List<ContactPhone> phone) {
		this.phone = phone;
	}

	public Bitmap getPhoto() {
		Bitmap photo = null;
		photo = loadImageFromUrl(context, photo_id);
		return photo;
	}

	public ContactPerson(Context context) {
		this.context = context;
	}

	public synchronized static Bitmap loadImageFromUrl(Context ct, String photo_id) {
		Bitmap d = null;
		if (photo_id == null || photo_id.equals(""))
			return d;
		try {
			String[] projection = new String[] { ContactsContract.Data.DATA15 };
			String selection = "ContactsContract.Data._ID = " + photo_id;
			Cursor cur = ct.getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, selection, null, null);
			cur.moveToFirst();
			byte[] contactIcon = null;
			if (cur.getBlob(cur.getColumnIndex(ContactsContract.Data.DATA15)) != null) {
				contactIcon = cur.getBlob(cur.getColumnIndex(ContactsContract.Data.DATA15));
				d = BitmapFactory.decodeByteArray(contactIcon, 0, contactIcon.length);
			}
			cur.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}

	public void addPhone(ContactPhone phone) {
		this.phone.add(phone);
	}

}
